<?php
/**
* $Id$
* @author Hung Nguyen, Flexica Solutions
*/
Yii::import('application.controllers.BackOfficeController');
class UserController extends BackOfficeController
{    
    /**
    * The default action is to list users
    *
    */
	public function actionIndex(){
        $this->actionList();
	}

    public function actionList(){
        $model = new User('search');
        if(isset($_GET['User']))
            $model->attributes=$_GET['User'];
            
        if (Yii::app()->request->isAjaxRequest)
        {
            $this->renderPartial('_grid', array('model' => $model));
            Yii::app()->end();
        }

        $this->render('list', array('model' => $model));
    }

    public function actionCreate()
    {
        $user = new User();
        $user->scenario = 'register';
        $data = array();
        
        if (Yii::app()->request->isPostRequest && isset($_POST['User']))
        {
            $user->setAttributes($_POST['User'],false);
            $user->rePassword = $_POST['User']['rePassword'];
            if ($user->validate())
            {
                $user->Password = md5($user->Password);
                // ngay hien tai
                $user->CreatedDate = date('Y-m-d H:i:s');
                $user->Id = null;
                if ($user->save(FALSE))
                {
                    $data['createSuccess'] = true;
                    $user = new User();
                    $user->scenario = 'register';    
                }   
            }
        }
        
        $data['user'] = $user;
        $this->render('edit', $data);
    }

    /**
    * Edit user
    * User query string Id parameter as the User ID, if Id=0 or not provided
    * the action will create a new user
    */
	public function actionEdit()
	{
	    $user = null;
        $id = '';
        if (isset($_GET['id']))
            $id = trim($_GET['id']);
        $user = User::model()->findByPk($id);
        $data = array();
        
        if (!is_null($user))
        {
            $user->scenario = 'edit';
            $user->Password = '';    
        }
        
        if (Yii::app()->request->isPostRequest && isset($_POST['User']) && !is_null($user))
        {
            $user->setAttributes($_POST['User'],false);
            $user->rePassword = $_POST['User']['rePassword'];
            if ($user->validate())
            {
                $updatedFields = array('Username','Name','Email','roles','Status');
                if ($user->Password != '')
                {
                    $user->Password = md5($user->Password);
                    $updatedFields[] = 'Password';
                }
                if ($user->update($updatedFields))
                {
                    $data['editSuccess'] = true;
                    $user->scenario = 'edit';
                    $user->Password = $user->rePassword = '';    
                }   
            }    
        }
        
        $data['user'] = $user;
        $this->render('edit', $data);
	}
    
    public function actionActivateOrBanUser()
    {
        if (Yii::app()->request->isAjaxRequest)
        {
            if (!isset($_POST['user_id']) || !isset($_POST['type']))
            {
                echo "(".json_encode(array('success' => 0,'message'=>'Dữ liệu chưa cung cấp đủ')).")";
                Yii::app()->end();     
            }
            
            $user_id = intval($_POST['user_id']);
            $type = trim($_POST['type']);
            
            if ($type != 'activate' && $type != 'ban')
            {
                echo "(".json_encode(array('success' => 0,'message'=>'Dữ liệu sai')).")";
                Yii::app()->end();
            }
            
            $user = User::model()->findByPk($user_id);
            if (is_null($user))
            {
                echo "(".json_encode(array('success' => 0,'message'=>'Tài khoản này không tồn tại')).")";
                Yii::app()->end();    
            }
            
            if ($type == 'activate')
                $user->Status = 1;
            else
                $user->Status = 0;
                
            if ($user->update(array('Status')))
                echo "(".json_encode(array('success' => 1)).")";
            else
                echo "(".json_encode(array('success' => 0,'message'=>'Cập nhật thất bại')).")";       
        }
    }
}
?>
